﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Activation;
using FeCMS.AccessControlModule.Security;
using FeCMS.Common.Enum;


namespace FeCMS.AccessControlModule.ContextAccess
{
    [AttributeUsage(AttributeTargets.Method)]
    public class PermissionAttribute : Attribute
    {
        private int _permissionID;

        public PermissionAttribute(int permissionID)
        {
            _permissionID = permissionID;
        }


        ///// <summary>
        /////    重载ContextAttribute方法，创建一个上下文环境属性
        ///// </summary>
        ///// <param name="ctorMsg"></param>
        //public override void GetPropertiesForNewContext(IConstructionCallMessage ctorMsg)
        //{
        //    ctorMsg.ContextProperties.Add(new PermissionProperty());
        //}

        public bool ValidPermission()
        {
            if (System.Web.HttpContext.Current.Request.IsAuthenticated)
            {
                var user = System.Web.HttpContext.Current.User as FeFormsPrincipal;
                //TODO:DLK 暂时只完成管理员,跟登录用户的权限
                switch (user.UserData.VisitorsType)
                {
                    case VisitorsType.admin:
                        return Manager.Instance.HasAdminPermission(user.UserData.UserName, _permissionID);
                    case VisitorsType.user:
                    case VisitorsType.webCrawler:
                    case VisitorsType.guest:
                        return Manager.Instance.HasPermission(user.UserData.RoleId, _permissionID);
                    default:
                        return false;
                }
            }

            return false;
        }

    }
}
